define(function(require, exports, module) {
    'use strict';
    
    var rk = require('rk');
    
    
    require('../su/accordion/oaTabCtrl');
    require('../topic/topicOperationCtrl');
    //var uService = require('core/services/noticeService');
    //var getTimeToString = uService.getTimeToString;
    var UE = require('lib/ueditor/ueditor.all');
    
    $.widget('rk.blogUpdateCtrl', $.rk.oaDialogCtrl, {
        _params:{},
        _create: function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;

        },
        _listeners:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;

        },
        _init:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var temp = require("oa/tmpl/blog/tpl_blog_update.tpl");
            rk.post("/json/oa_blog/_to-blog-modify.action",{blogId:opt.bid},function(json){
                if(json.status === 0) {
                    var html = rk.templateText(temp, json);
                    elem.html(html);
                    var editor = new UE.ui.Editor({initialFrameHeight:300,initialFrameWidth:770});
                    editor.render('blogWriteEditor');
                    me.editor = editor;
                    editor.addListener("ready", function () {
                        // editor准备好之后才可以使用
                        editor.setContent(json.blog.content);
                    });
                }else{
                    rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                }
            });

            var footer = me.getFooter();
            me.getBtn('ok').on('click', $.proxy(me.okClicked, me));
            footer.find('*[act="cancel"]').on('click', $.proxy(me.cancelClicked, me));
        },
        okClicked: function () {
            var me = this;
            var elem = me.element;
            me._doSave();//保存
        },
        cancelClicked: function () {
            var me = this;
            var elem = me.element;
            me.closeMe();//关闭
        },
        _doSave: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var editor = me.editor;
            editor.sync("blog_add_save_from");
            var form = elem.find("[act='blog_add_save_from']");
            var datas = form.serializeArray();
            var params = {};
            $.each(datas, function (i, json) {
                if(json.name == "title" || json.name == "id"){
                    params["blog."+json.name] = json.value;
                }else {
                    params[json.name] = json.value;
                }
            });
            if(!params["blog.title"]){
                rk.noticeError(rk.i18n('ANNOUNCEMENT_1'));
                return;
            }
            if(!params["content"]){
                rk.noticeError(rk.i18n('ANNOUNCEMENT_2'));
                return;
            }
            var content = editor.getContent();
            if(content.length > 8000000){
                rk.noticeError(rk.i18n('ANNOUNCEMENT_3'));
                return;
            }
            me.getBtn('ok').beLoading();
            rk.post("/json/oa_blog/blog-modify.action",params,function(json){
                if(json.status === 0){
                    me.closeMe();
                    rk.noticeSuccess(rk.i18n('OPERATE_SUCCESS'));
                    history.go(0);
                }else if(json.status === 217001){
                    rk.noticeError(rk.i18n('GROUP_NO_PERMISSION'));
                    me.getBtn('ok').beLoading('delay');
                }else{
                    rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                    me.getBtn('ok').beLoading('delay');
                }
            });
        }
    });
});
